package util;

import java.awt.BorderLayout;

import javax.swing.BoundedRangeModel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JProgressBar;

public class progressDialog extends Thread {
	private JDialog dlg;
	private JProgressBar dpb;
	private int value = 0;
	private String tittle;
	private int elementsCount = 0;

	public progressDialog(String mtittle, int melementsCount) {
		tittle = mtittle;
		elementsCount = melementsCount - 1;
		dlg = new JDialog();// "Progress Dialog", true);
		dlg.setTitle(tittle);
		dpb = new JProgressBar(0, elementsCount);
		dpb.setStringPainted(true);
		dlg.add(BorderLayout.CENTER, dpb);
		dlg.add(BorderLayout.NORTH, new JLabel("Progress..."));
		dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dlg.setSize(300, 75);
		dpb.setValue(50);
		dlg.setAlwaysOnTop(true);
	}

	public progressDialog(String mtittle) {
		tittle = mtittle;
		dlg = new JDialog();// "Progress Dialog", true);
		dlg.setTitle(tittle);
		dpb = new JProgressBar();
		dlg.add(BorderLayout.CENTER, dpb);
		dlg.add(BorderLayout.NORTH, new JLabel("Loading..."));
		dlg.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dlg.setSize(300, 75);
		dpb.setIndeterminate(true);
		dlg.setAlwaysOnTop(true);
	}

	@Override
	public void run() {
		// super.run();
		dlg.setVisible(true);
		/*
		 * while (true) { dpb.setValue(value); System.out.println("" + value);
		 * dpb.revalidate(); dlg.repaint(); try { Thread.sleep(1000); } catch
		 * (InterruptedException e) { e.printStackTrace(); } }
		 */
	}

	public void setProgressDialoj(int i) {
		value = i;
		dpb.setValue(i);
		if (i == elementsCount) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dlg.dispose();
		}
	}

	public void closeProgressDialog() {
		dlg.dispose();
	}
}
